package com.dlc.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.app.dto.SkuDto;
import com.dlc.shop.bean.app.vo.SkuVO;
import com.dlc.shop.bean.dto.StockBillLogItemDTO;
import com.dlc.shop.bean.dto.StockPointSkuDTO;
import com.dlc.shop.bean.dto.WarehouseDTO;
import com.dlc.shop.bean.enums.StockBillType;
import com.dlc.shop.bean.enums.StockModeEnum;
import com.dlc.shop.bean.enums.StockType;
import com.dlc.shop.bean.event.StockBillChangeEvent;
import com.dlc.shop.bean.model.*;
import com.dlc.shop.bean.vo.SkuStockVO;
import com.dlc.shop.bean.vo.StockPointSkuVO;
import com.dlc.shop.bean.vo.WarehouseVO;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.constants.CacheNames;
import com.dlc.shop.common.constants.LuaOperateEnum;
import com.dlc.shop.common.constants.StockPointType;
import com.dlc.shop.common.enums.StatusEnum;
import com.dlc.shop.common.enums.SysTypeEnum;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.dao.*;
import com.dlc.shop.manager.impl.StockManager;
import com.dlc.shop.service.SkuStockService;
import com.dlc.shop.service.StockPointSkuService;
import com.dlc.shop.service.WarehouseService;
import com.dlc.shop.util.StockUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 库存信息
 *
 * @author LGH
 * @date 2022-05-06 14:59:42
 */
@Slf4j
@Service
@AllArgsConstructor
public class SkuStockServiceImpl implements SkuStockService {
    private final StockManager stockManager;
    private final SkuMapper skuMapper;
    private final ProductMapper productMapper;
    private final StockPointSkuService stockPointSkuService;
    private final WarehouseService warehouseService;
    private final ApplicationContext applicationContext;
    private final StockBillLogItemMapper stockBillLogItemMapper;
    private final OrderItemMapper orderItemMapper;
    private final StockPointSkuMapper stockPointSkuMapper;

    @Override
    public List<SkuStockVO> listSkuStockBySkuIds(List<StockPointSkuVO> stockPointIdList) {
        return stockManager.convertSkuStock(stockPointIdList);
    }

    @Override
    public Map<String, SkuStockVO> mapSkuTotalStockAndPointStock(List<StockPointSkuVO> stockPointSkuList) {
        Set<Long> skuIdSet = stockPointSkuList.stream().map(StockPointSkuVO::getSkuId).collect(Collectors.toSet());
        for (Long skuId : skuIdSet) {
            StockPointSkuVO stockPointSkuVO = new StockPointSkuVO();
            stockPointSkuVO.setSkuId(skuId);
            stockPointSkuVO.setStockPointId(Constant.ZERO_LONG);
            stockPointSkuList.add(stockPointSkuVO);
        }
        return stockManager.skuStockMap(stockPointSkuList);
    }

    @Override
    public Map<Long, SkuStockVO> mapSkuTotalStock(List<Long> skuIds) {
        if (CollUtil.isEmpty(skuIds)) {
            return Collections.emptyMap();
        }
        return stockManager.mapSkuTotalStock(skuIds);
    }

    /**
     * 处理一下库存关联商品数据，分别计算新增，修改，删除
     *
     * @param skuStockList
     * @param spuId
     * @param luaOperateEnum 区分修改的添加还是替换
     */
    @Override
    public void handSkuStock(Long spuId, List<SkuStockVO> skuStockList, LuaOperateEnum luaOperateEnum, Boolean hasUserPickUp, Long shopId) {
        // 获取之前的关联数据
        List<Sku> skus = skuMapper.listByProdId(spuId);
        List<Long> skuIds = skus.stream().map(Sku::getSkuId).collect(Collectors.toList());
        List<StockPointSkuVO> stockPointList = stockPointSkuService.listPointIdsBySkuIds(skuIds);
        Map<String, SkuStockVO> skuStockMap = this.mapSkuPointStockByStockPoints(stockPointList);
        Long deafultId = warehouseService.getDefaultWarehouseByShopId(shopId, Objects.equals(shopId, Constant.PLATFORM_SHOP_ID) ? SysTypeEnum.PLATFORM.value() : SysTypeEnum.MULTISHOP.value()).getWarehouseId();
        List<SkuStockVO> updateList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<SkuStockVO> deleteKey = new ArrayList<>(Constant.INITIAL_CAPACITY);
        // 需要删除的关联库存点
        List<Long> deleteSkuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SkuStockVO skuStockVO : skuStockList) {
            String key = StockUtil.skuPointKey(skuStockVO.getSkuId(), skuStockVO.getStockPointId());
            SkuStockVO skuStockRb = skuStockMap.remove(key);
            if (skuStockVO.getStockPointId() == 0) {
                // 总库存不需要修改
                continue;
            }
            // 获取数据的时候将sku中包含的库存删掉，剩下不包含在列表中的库存就是要删除的
            if (!skuStockVO.getStockPointId().equals(deafultId) && (skuStockVO.getStock() == 0 || skuStockVO.getStock() == null)) {
                deleteKey.add(new SkuStockVO(skuStockVO.getSkuId(), 0, skuStockVO.getStockPointId(), null));
                continue;
            }
            if (Objects.isNull(skuStockRb) || Objects.equals(skuStockRb.getStatus(), StatusEnum.DELETE.value())) {
                // 插入
                skuStockVO.setStatus(StatusEnum.ENABLE.value());
                skuStockVO.setOperateType(LuaOperateEnum.SKU_INSERT.value());
                updateList.add(skuStockVO);
            } else {
                // 更新
                skuStockVO.setOperateType(luaOperateEnum.value());
                updateList.add(skuStockVO);
            }
        }
        // 此处都是sku库存列表中没有包含的区域库存信息，全部删除 - 不要用stockManager.mDel()去删，用lua脚本，不然sku总库存数量不会同步更改
        for (Map.Entry<String, SkuStockVO> entry : skuStockMap.entrySet()) {
            SkuStockVO skuStockVO = entry.getValue();
            SkuStockVO stockVO = new SkuStockVO(skuStockVO.getSkuId(), skuStockVO.getStock(), skuStockVO.getStockPointId(), null);
            for (StockPointSkuVO pointSkuVO : stockPointList) {
                if (pointSkuVO.getSkuId().equals(skuStockVO.getSkuId()) && pointSkuVO.getStockPointId().equals(skuStockVO.getStockPointId())) {
                    deleteSkuIds.add(pointSkuVO.getStockPointSkuId());
                    stockVO.setStock(0);
                }
            }
            deleteKey.add(stockVO);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            String updateStock = stockManager.skuStock(updateList, null);
            if (!updateStock.contains(Constant.UNDERLINE)) {
                throw new YamiShopBindException("yami.sku.stock.change");
            }
        }
        if (luaOperateEnum == LuaOperateEnum.SKU_REPLACE) {
            // 如果是新增的话，就不需要删除之前的
            stockManager.skuStock(deleteKey, LuaOperateEnum.SKU_REPLACE);
            stockPointSkuService.batchDeleteByStockPointSkuIdList(deleteSkuIds);
        }
        List<SkuStockVO> statusList = skuStockList.stream().filter(skuStockVO ->
                Objects.nonNull(skuStockVO.getStockPointType()) && skuStockVO.getStockPointType() == StockPointType.STATION.getValue()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(statusList)) {
            return;
        }
        if (hasUserPickUp) {
            statusList.forEach(s -> s.setStatus(1));
        } else {
            // 如果没开启自提，门店库存下架
            statusList.forEach(s -> s.setStatus(0));
        }
        // 门店库存更新一下状态
        String updateStock = stockManager.skuStock(statusList, LuaOperateEnum.SKU_POINT_STATUS);
        if (!updateStock.contains(Constant.UNDERLINE)) {
            throw new YamiShopBindException("yami.sku.stock.change");
        }
    }

    @Override
    public Map<String, SkuStockVO> mapSkuPointStockByStockPoints(List<StockPointSkuVO> stockPointSkuList) {
        return stockManager.skuStockMap(stockPointSkuList);
    }

    @Override
    public void changeSkuStock(List<SkuStockVO> skuStockVOList) {
        if (CollectionUtils.isEmpty(skuStockVOList)) {
            return;
        }
        Set<Long> stockPointIds = new HashSet<>(Constant.INITIAL_CAPACITY);
        Set<Long> skuIds = new HashSet<>(Constant.INITIAL_CAPACITY);
        for (SkuStockVO skuStockVO : skuStockVOList) {
            stockPointIds.add(skuStockVO.getStockPointId());
            skuIds.add(skuStockVO.getSkuId());
        }
        // 查询仓库
        WarehouseDTO warehouseDTO = new WarehouseDTO();
        warehouseDTO.setWarehouseIds(new ArrayList<>(stockPointIds));
        List<WarehouseVO> warehouses = warehouseService.listWarehouse(warehouseDTO);
        if (CollectionUtils.isEmpty(warehouses)) {
            throw new YamiShopBindException("yami.warehouse.null");
        }
        Map<Long, WarehouseVO> warehouseMap = warehouses.stream().collect(Collectors.toMap(WarehouseVO::getWarehouseId, x -> x));
        // 查询商品sku
        SkuDto skuDTO = new SkuDto();
        skuDTO.setSkuIds(new ArrayList<>(skuIds));
        List<SkuVO> skus = skuMapper.listSkuWithLang(skuDTO);
        if (CollectionUtils.isEmpty(skus)) {
            return;
        }
        Map<Long, SkuVO> skuMap = skus.stream().collect(Collectors.toMap(SkuVO::getSkuId, x -> x));
        // 查询redis区域库存
        Map<String, SkuStockVO> skuStockMap = stockManager.mapSkuStockBySkuStocks(skuStockVOList);
        // 查询仓库与商品关联（转成Map，key格式为：skuId_stockPointId）
        StockPointSkuDTO stockPointSkuDTO = new StockPointSkuDTO();
        stockPointSkuDTO.setSkuIds(new ArrayList<>(skuIds));
        stockPointSkuDTO.setStockPointIds(new ArrayList<>(stockPointIds));
        List<StockPointSkuVO> stockPointSkus = stockPointSkuService.listStockPointSku(stockPointSkuDTO);
        Map<String, StockPointSkuVO> stockPointSkuMap = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (!CollectionUtils.isEmpty(stockPointSkus)) {
            stockPointSkuMap = stockPointSkus.stream().collect(Collectors.toMap(k -> StockUtil.skuPointKey(k.getSkuId(), k.getStockPointId()), v -> v));
        }
        // 找出新增or删除的数据
        List<StockPointSkuDTO> insertData = new ArrayList<>(Constant.INITIAL_CAPACITY);
        List<Long> deleteData = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (SkuStockVO skuStockVO : skuStockVOList) {
            WarehouseVO warehouseVO = warehouseMap.get(skuStockVO.getStockPointId());
            if (Objects.isNull(warehouseVO)) {
                throw new YamiShopBindException(String.format("仓库[%s]不存在", skuStockVO.getStockPointId()));
            }
            SkuVO skuVO = skuMap.get(skuStockVO.getSkuId());
            if (Objects.isNull(skuVO)) {
                continue;
            }
            String key = StockUtil.skuPointKey(skuStockVO.getSkuId(), skuStockVO.getStockPointId());
            SkuStockVO skuStock = skuStockMap.get(key);
            StockPointSkuVO stockPointSku = stockPointSkuMap.get(key);
            if (Objects.equals(skuStockVO.getOperateType(), LuaOperateEnum.SKU_ADD.value())) {
                // 增加库存 --- 库存不存在，插入redis库存；关联不存在，新增仓库商品关联
                if (Objects.isNull(skuStock)
                        || Objects.equals(skuStock.getStatus(), -1)) {
                    skuStockVO.setOperateType(LuaOperateEnum.SKU_INSERT.value());
                    skuStockVO.setStatus(skuVO.getStatus());
                }
                if (Objects.isNull(stockPointSku)) {
                    stockPointSkuDTO = new StockPointSkuDTO();
                    BeanUtils.copyProperties(skuStockVO, stockPointSkuDTO);
                    stockPointSkuDTO.setProdId(skuStockVO.getProdId());
                    stockPointSkuDTO.setStatus(skuVO.getStatus());
                    stockPointSkuDTO.setType(warehouseVO.getType());
                    stockPointSkuDTO.setStockPointType(Objects.equals(warehouseVO.getSysType(), SysTypeEnum.STATION.value()) ? StockPointType.STATION.getValue() : StockPointType.WAREHOUSE.getValue());
                    insertData.add(stockPointSkuDTO);
                }
            } else if (Objects.equals(skuStockVO.getOperateType(), LuaOperateEnum.SKU_SUB.value())) {
                // 减少库存 --- 判断库存是否充足；若扣减后库存为0，非默认仓库需要删除仓库商品关联
                if (Objects.isNull(skuStock)
                        || skuStock.getStock() < skuStockVO.getStock()) {
                    throw new YamiShopBindException(String.format("仓库[%s]中skuId[%s]库存不足", skuStockVO.getStockPointId(), skuStockVO.getSkuId()));
                }
                if (Objects.equals(skuStock.getStock(), skuStockVO.getStock())
                        && !Objects.equals(warehouseVO.getType(), 0)
                        && !Objects.isNull(stockPointSku)) {
                    deleteData.add(stockPointSku.getStockPointSkuId());
                }
            }
        }
        // 新增仓库商品关联数据
        if (!CollectionUtils.isEmpty(insertData)) {
            log.info("新增仓库商品关联数据");
            stockPointSkuService.batchSave(insertData);
        }
        // 删除仓库商品关联数据
        if (!CollectionUtils.isEmpty(deleteData)) {
            log.info("删除仓库商品关联数据");
            stockPointSkuService.deleteByIds(deleteData);
        }
        // 修改redis库存
        stockManager.skuStock(skuStockVOList, null);
    }

    @Override
    public Map<String, SkuStockVO> listSkuStock(List<SkuStockVO> skuStocks) {
        // 转成Map形式方便获取(key: skuId_stockPontId)
        return stockManager.mapSkuStockBySkuStocks(skuStocks);
    }

    @Override
    public List<Long> checkStockPointContainSku(List<Long> stockPointIds, List<Long> skuIds) {
        StockPointSkuDTO stockPointSkuDTO = new StockPointSkuDTO();
        stockPointSkuDTO.setStockPointIds(stockPointIds);
        List<StockPointSkuVO> stockPointSkus = stockPointSkuService.listStockPointSku(stockPointSkuDTO);
        if (CollectionUtils.isEmpty(stockPointSkus)) {
            return new ArrayList<>();
        }
        // 获取库存点含有的skuId
        List<Long> containSkuIds = new ArrayList<>(Constant.INITIAL_CAPACITY);
        Set<Long> dbSkuIds = stockPointSkus.stream().map(StockPointSkuVO::getSkuId).collect(Collectors.toSet());
        for (Long skuId : skuIds) {
            if (!dbSkuIds.contains(skuId)) {
                continue;
            }
            containSkuIds.add(skuId);
        }
        return containSkuIds;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAndUpdateStockPoint(Long spuId, List<StockPointSku> stockPointSkuList, Long shopId) {
        if (CollectionUtils.isEmpty(stockPointSkuList)) {
            return;
        }
        // 过滤一下已经被删除的仓库和门店或改为共享模式的门店
        List<StockPointSku> filtePointList = stockPointSkuService.filtePoint(stockPointSkuList);
        // 处理关联数据
        List<StockPointSkuDTO> pointSkuDTOS = BeanUtil.mapAsList(filtePointList, StockPointSkuDTO.class);
        stockPointSkuService.changeStockPoint(pointSkuDTOS, false);
        // 修改redis缓存
        List<SkuStockVO> skuStockList = new ArrayList<>(filtePointList.size());
        for (StockPointSkuDTO stockPointSku : pointSkuDTOS) {
            skuStockList.add(new SkuStockVO(stockPointSku.getSkuId(), stockPointSku.getStock(), null, null, stockPointSku.getStockPointId()));
        }
        handSkuStock(spuId, skuStockList, LuaOperateEnum.SKU_ADD, true, shopId);
        // 生成入库记录
        loadSeckillStockBillLogItems(pointSkuDTOS, shopId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddAndUpdateStockPoint(List<StockPointSku> stockPointSkuList) {
        if (CollectionUtils.isEmpty(stockPointSkuList)) {
            return;
        }
        // 过滤一下已经被删除的仓库和门店或改为共享模式的门店
        List<StockPointSku> filtePointList = stockPointSkuService.filtePoint(stockPointSkuList);
        // 处理关联数据
        List<StockPointSkuDTO> pointSkuDTOS = BeanUtil.mapAsList(filtePointList, StockPointSkuDTO.class);
        stockPointSkuService.changeStockPoint(pointSkuDTOS, false);
        // 修改redis缓存
        List<SkuStockVO> skuStockList = new ArrayList<>(filtePointList.size());
        for (StockPointSkuDTO stockPointSku : pointSkuDTOS) {
            skuStockList.add(new SkuStockVO(stockPointSku.getSkuId(), stockPointSku.getStock(), null, null, stockPointSku.getStockPointId()));
        }
        Map<Long, List<StockPointSku>> spuMap = stockPointSkuList.stream().collect(Collectors.groupingBy(StockPointSku::getProdId));
        for (Map.Entry<Long, List<StockPointSku>> entry : spuMap.entrySet()) {
            Long shopId = entry.getValue().get(0).getShopId();
            handSkuStock(entry.getKey(), skuStockList, LuaOperateEnum.SKU_ADD, true, shopId);
            // 生成入库记录
            loadSeckillStockBillLogItems(pointSkuDTOS, shopId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStockMode(Long stockPointId, Integer stockMode, Integer type, Long shopId, Long employeeId) {
        // 获取门店所有的sku
        List<StockPointSkuVO> pointSkuList = stockPointSkuService.listByPointId(stockPointId);
        if (Objects.nonNull(type)) {
            List<Long> skuIds = pointSkuList.stream().map(StockPointSkuVO::getSkuId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(skuIds)) {
                return;
            }
            // 获取原来的sku库存
            List<SkuStockVO> stockDbList = listSkuStockBySkuIds(pointSkuList);
            Map<Long, Integer> stockMap = stockDbList.stream().filter(skuStockVO -> skuStockVO.getStockPointId() != 0).collect(Collectors.toMap(SkuStockVO::getSkuId, SkuStockVO::getStock));
            pointSkuList.forEach(s -> s.setStock(stockMap.get(s.getSkuId())));
            List<SkuStockVO> deleteList = new ArrayList<>(pointSkuList.size());
            for (StockPointSkuVO skuVO : pointSkuList) {
                SkuStockVO skuStockVO = new SkuStockVO(skuVO.getSkuId(), stockMap.get(skuVO.getSkuId()), skuVO.getStockPointId(), null);
                deleteList.add(skuStockVO);
            }
            if (type == 1) {
                // 添加默认仓库库存
                List<SkuStockVO> updateList = new ArrayList<>(skuIds.size());
                // 获取sku对应的默认仓库
                List<StockPointSkuVO> pointList = stockPointSkuService.listPointBySkuIds(skuIds).stream().filter(stockPointSkuVO -> stockPointSkuVO.getType() == 0).toList();
                // 将门店库存添加到默认仓库
                for (StockPointSkuVO pointSkuVO : pointList) {
                    SkuStockVO skuStockVO = new SkuStockVO(pointSkuVO.getSkuId(), stockMap.get(pointSkuVO.getSkuId()), pointSkuVO.getStockPointId(), null);
                    updateList.add(skuStockVO);
                }
                stockManager.skuStock(updateList, LuaOperateEnum.SKU_ADD);
            }
            stockManager.skuStock(deleteList, LuaOperateEnum.SKU_SUB);
            List<StockBillLogItemDTO> stockBillLogItemDTOList = new ArrayList<>();
            List<Sku> skuList = skuMapper.listSkuBySkuIds(skuIds);
            addBillLogList(employeeId, null, SysTypeEnum.MULTISHOP.value(), StockBillType.SWITCH_STATION.value(), StockType.OUT_OF_STOCK.value(), BeanUtil.mapAsList(pointSkuList, StockPointSkuDTO.class), skuList, shopId, null);
            if (type == 1) {
                // 获取默认仓库
                WarehouseVO warehouse = warehouseService.getDefaultWarehouseByShopId(shopId, SysTypeEnum.MULTISHOP.value());
                if (Objects.isNull(warehouse)) {
                    throw new YamiShopBindException("yami.warehouse.default.null");
                }
                addBillLogList(employeeId, null, SysTypeEnum.MULTISHOP.value(), StockBillType.SWITCH_WAREHOUSE.value(), StockType.WAREHOUSING.value(), BeanUtil.mapAsList(pointSkuList, StockPointSkuDTO.class), skuList, shopId, warehouse.getWarehouseId());
            }
        }
        List<Long> stockPointSkuIds = pointSkuList.stream().map(StockPointSkuVO::getStockPointSkuId).collect(Collectors.toList());
        stockPointSkuService.batchDeleteByStockPointSkuIdList(stockPointSkuIds);
        warehouseService.removeWarehouseCache(stockPointId, shopId, SysTypeEnum.MULTISHOP.value());
    }

    @Override
    public void addBillLogList(Long employeeId, String mobile, Integer sysType, int stockBillType, int stockType, List<StockPointSkuDTO> stockPointSkuList, List<Sku> skus, Long shopId, Long stockPointId) {
        if (CollUtil.isEmpty(stockPointSkuList)) {
            return;
        }
        List<StockBillLogItemDTO> stockBillLogItems = new ArrayList<>(stockPointSkuList.size());
        Map<Long, Sku> skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuId, s -> s));
        stockPointSkuList.forEach(s -> {
            Sku sku = skuMap.get(s.getSkuId());
            if (Objects.isNull(sku)) {
                return;
            }
            StockBillLogItemDTO stockBillLogItem = new StockBillLogItemDTO(
                    s.getProdId(),
                    s.getSkuId(),
                    shopId,
                    null,
                    sku.getProdName(),
                    sku.getSkuName(),
                    Objects.isNull(s.getStock()) ? 0 : s.getStock(),
                    sku.getPrice(),
                    sku.getPartyCode(),
                    sku.getPic()
            );
            stockBillLogItem.setAfterStock(stockBillLogItem.getStockCount());
            stockBillLogItem.setStockBillType(stockBillType);
            stockBillLogItem.setStocktype(stockType);
            stockBillLogItem.setStockPointId(stockPointId == null ? s.getStockPointId() : stockPointId);
            stockBillLogItem.setStockPointType(stockPointId == null ? s.getStockPointType() : StockPointType.WAREHOUSE.getValue());
            stockBillLogItem.setMakerMobile(mobile);
            stockBillLogItem.setEmployeeId(employeeId);
            stockBillLogItems.add(stockBillLogItem);
        });
        // 计算剩余库存
        try {
            List<SkuStockVO> skuStockList = new ArrayList<>();
            for (StockBillLogItemDTO stockBillLogItem : stockBillLogItems) {
                if (Objects.equals(stockBillType, StockBillType.INITIALIZE.value())) {
                    continue;
                }
                skuStockList.add(new SkuStockVO(stockBillLogItem.getSkuId(), stockBillLogItem.getStockPointId()));
            }
            if (CollUtil.isNotEmpty(skuStockList)) {
                Map<String, SkuStockVO> stockMap = stockManager.mapSkuStockBySkuStocks(skuStockList);
                for (StockBillLogItemDTO stockBillLogItem : stockBillLogItems) {
                    String key = StockUtil.skuPointKey(stockBillLogItem.getSkuId(), stockBillLogItem.getStockPointId());
                    SkuStockVO skuStockVO = stockMap.get(key);
                    int afterStock = skuStockVO.getStock();
                    if (Objects.equals(stockType, StockType.OUT_OF_STOCK.value())) {
                        afterStock -= stockBillLogItem.getStockCount();
                    } else {
                        afterStock += stockBillLogItem.getStockCount();
                    }
                    stockBillLogItem.setAfterStock(afterStock);
                }
            }
        } catch (Exception e) {
            log.info("计算剩余库存异常：{}", e.getMessage());
        }
        applicationContext.publishEvent(new StockBillChangeEvent(stockBillLogItems, stockBillType, stockType));
    }

    @Override
    public Map<Long, Integer> getStockMapBySkuIdAndStockPointIds(Long skuId, List<Long> stockPointIds) {
        List<StockPointSkuVO> stockPointSkuList = new ArrayList<>(stockPointIds.size());
        for (Long stockPointId : stockPointIds) {
            StockPointSkuVO stockPointSkuVO = new StockPointSkuVO();
            stockPointSkuVO.setStockPointId(stockPointId);
            stockPointSkuVO.setSkuId(skuId);
            stockPointSkuList.add(stockPointSkuVO);
        }
        List<SkuStockVO> skuStockVOList = listSkuStockBySkuIds(stockPointSkuList);
        return skuStockVOList.stream().collect(Collectors.toMap(SkuStockVO::getStockPointId, SkuStockVO::getStock));
    }

    @Override
    public void orderRefundRecoveryStock(List<SkuStockVO> skuStockList) {
        // 扣除用户库存
        List<String[]> list = new ArrayList<>(skuStockList.size());
        for (SkuStockVO skuStockVO : skuStockList) {
            list.add(StockUtil.loadQueryArray(
                    skuStockVO.getSkuId(),
                    skuStockVO.getStock(),
                    skuStockVO.getStockPointId(),
                    skuStockVO.getRefundId(),
                    skuStockVO.getTime().getTime())
            );
        }
        stockManager.executeLuk(list, StockManager.REFUND_INCREASE_STOCK_BYTES);
    }

    @Override
    public void initStock(Integer initStock) {
        int index = 0;
        // mset数量定为50， 数量太大redis性能衰减
        int size = 100;
        List<Sku> skuList = skuMapper.selectList(new LambdaQueryWrapper<Sku>().ne(Sku::getStatus, StatusEnum.DELETE.value()));
        Map<Long, Integer> skuMap = skuList.stream().collect(Collectors.toMap(Sku::getSkuId, Sku::getStatus));
        List<StockPointSku> stockPointSkuList = stockPointSkuService
                .list(new LambdaQueryWrapper<StockPointSku>().eq(StockPointSku::getType, 0).in(StockPointSku::getSkuId, skuMap.keySet()));
        Map<Long, List<StockPointSku>> stockPointMap = stockPointSkuList.stream().collect(Collectors.groupingBy(StockPointSku::getSkuId));
        while (index < skuList.size()) {
            int toIndex = size + index;
            if (toIndex > skuList.size()) {
                toIndex = skuList.size();
            }
            List<Sku> skus = skuList.subList(index, toIndex);
            if (CollUtil.isEmpty(skus)) {
                break;
            }
            List<Long> skuIds = skus.stream().map(Sku::getSkuId).toList();
            Map<Long, Integer> stockDbMap = new HashMap<>(0);
            try {
                // 查询sku库存
                List<SkuStock> skuStocks = skuMapper.listSkuStock(skuIds);
                stockDbMap = skuStocks.stream().collect(Collectors.toMap(SkuStock::getSkuId, SkuStock::getStocks));
            } catch (Exception e) {
                log.error("获取sku库存异常", e);
            }
            Map<String, SkuStockVO> skuStockMap = stockManager.scanSkuStock(CacheNames.SKU_SOCK);
            Map<String, String> map = new HashMap<>(135);
            List<SkuStockVO> skuStockList = new ArrayList<>(Constant.INITIAL_CAPACITY);
            // 拼接key和value
            for (Sku sku : skus) {
                if (!stockPointMap.containsKey(sku.getSkuId())) {
                    continue;
                }
                int stock = 0;
                int lockStock = 0;
                int sale = 0;
                int status = 0;
                if (Objects.equals(sku.getStatus(), StatusEnum.ENABLE.value())) {
                    status = StatusEnum.ENABLE.value();
                }
                if (!stockPointMap.containsKey(sku.getSkuId())) {
                    continue;
                }
                List<StockPointSku> stockPointSkus = stockPointMap.get(sku.getSkuId());
                for (StockPointSku stockPointSku : stockPointSkus) {
                    if (stockPointSku.getStatus() == -1) {
                        continue;
                    }
                    int pointStatus = status;
                    SkuStockVO skuPointStock = new SkuStockVO(sku.getSkuId(), initStock, 0, 0, stockPointSku.getStockPointId());
                    // 默认仓库、库存未初始化
                    if (Objects.equals(stockPointSku.getType(), 0) && stockDbMap.containsKey(sku.getSkuId())) {
                        skuPointStock.setStock(stockDbMap.get(sku.getSkuId()));
                        skuStockList.add(skuPointStock);
                    } else {
                        SkuStockVO skuStockPoint = skuStockMap.get(StockUtil.skuPointKey(sku.getSkuId(), stockPointSku.getStockPointId()));
                        if (Objects.nonNull(skuStockPoint) && !Objects.equals(skuStockPoint.getStatus(), StatusEnum.DELETE.value())) {
                            if (status > 0 && Objects.equals(stockPointSku.getStockMode(), StockModeEnum.ALL_STOCK.value())) {
                                pointStatus = 0;
                            }
                            skuPointStock.setStock(skuStockPoint.getStock());
                            if (Objects.nonNull(skuStockPoint.getLockStock())) {
                                skuPointStock.setLockStock(skuStockPoint.getLockStock());
                            }
                            if (Objects.nonNull(skuStockPoint.getSale())) {
                                skuPointStock.setSale(skuStockPoint.getSale());
                            }
                        }
                        if (Objects.isNull(skuStockPoint) || Objects.equals(skuStockPoint.getStatus(), StatusEnum.DELETE.value())) {
                            skuStockList.add(skuPointStock);
                        }
                    }
                    skuPointStock.setStatus(pointStatus);
                    lockStock += skuPointStock.getLockStock();
                    sale += skuPointStock.getSale();
                    stock = stock + skuPointStock.getStock();
                }
                SkuStockVO skuStock = new SkuStockVO(sku.getSkuId(), stock, lockStock, sale, 0L);
                skuStock.setStatus(status);
                skuStockList.add(skuStock);
            }
            for (SkuStockVO skuStockVO : skuStockList) {
                String key = CacheNames.SKU_SOCK + skuStockVO.getSkuId();
                if (skuStockVO.getStockPointId() > 0) {
                    key = key + StrUtil.C_UNDERLINE + skuStockVO.getStockPointId();
                }
                String value = skuStockVO.getStock().toString() +
                        StrUtil.C_UNDERLINE +
                        skuStockVO.getLockStock() +
                        StrUtil.C_UNDERLINE +
                        skuStockVO.getSale() +
                        StrUtil.C_UNDERLINE + skuStockVO.getStatus();
                map.put(key, value);
            }
            if (MapUtil.isNotEmpty(map)) {
                // 批量插入 mset
                stockManager.mSet(map);
            }
            index += size;
        }
    }

    @Override
    public Map<Long, Long> resetStock(List<SkuStockVO> skuStockList) {
        // 组装sku所在的店铺仓库、门店集合
        Set<Long> shopIdSet = skuStockList.stream().map(SkuStockVO::getShopId).collect(Collectors.toSet());
        List<WarehouseVO> warehouseDbList = warehouseService.listWarehouseAndStationByShopIds(new ArrayList<>(shopIdSet));
        Map<Long, List<WarehouseVO>> shopWarehouseMap = warehouseDbList.stream().collect(Collectors.groupingBy(WarehouseVO::getShopId));
        // 组装sku和库存的关联数据
        Set<Long> skuIdSet = skuStockList.stream().map(SkuStockVO::getSkuId).collect(Collectors.toSet());
        List<StockPointSkuVO> stockPointSkus = stockPointSkuService.listBySkuIds(new ArrayList<>(skuIdSet));
        Map<Long, List<StockPointSkuVO>> skuStockPointMap = stockPointSkus.stream().collect(Collectors.groupingBy(StockPointSkuVO::getSkuId));
        // 库存按店铺进行分组
        Map<Long, List<SkuStockVO>> shopStockMap = skuStockList.stream().collect(Collectors.groupingBy(SkuStockVO::getShopId));
        Map<String, StockPointSku> addStockMap = new HashMap<>(skuStockList.size());
        for (Map.Entry<Long, List<WarehouseVO>> entry : shopWarehouseMap.entrySet()) {
            Long shopId = entry.getKey();
            if (!shopStockMap.containsKey(shopId)) {
                continue;
            }
            List<WarehouseVO> warehouseVOList = entry.getValue();
            warehouseVOList.sort(Comparator.comparing(WarehouseVO::getType));
            Long defaultPointId = warehouseVOList.get(0).getWarehouseId();
            Map<Long, WarehouseVO> warehouseMap = warehouseVOList.stream().collect(Collectors.toMap(WarehouseVO::getWarehouseId, Function.identity()));
            List<SkuStockVO> skuStocks = shopStockMap.get(shopId);
            for (SkuStockVO skuStock : skuStocks) {
                boolean containsKey = warehouseMap.containsKey(skuStock.getStockPointId());
                // ===================== 仓库删除了，替换库存id为默认库存 =====================
                if (Objects.nonNull(skuStock.getStockPointId()) && skuStock.getStockPointId() != 0 && !containsKey) {
                    skuStock.setDefaultPointId(defaultPointId);
                    log.info("仓库已删除，更换为默认仓库, stockPointId: {}, 默认仓库：{}", skuStock.getStockPointId(), defaultPointId);
                }
                if (!skuStockPointMap.containsKey(skuStock.getSkuId())) {
                    continue;
                }
                // ===================== sku和仓库的关联重新绑定 =====================
                List<StockPointSkuVO> stockPointSkuList = skuStockPointMap.get(skuStock.getSkuId());
                long stockPointCount = stockPointSkuList.stream().filter(stockPointSkuVO -> Objects.equals(stockPointSkuVO.getStockPointId(), skuStock.getStockPointId())).count();
                String key = skuStock.getSkuId() + Constant.UNDERLINE + skuStock.getStockPointId();
                // sku和仓库的关联由于库存为0被删除，要重新添加关联
                if (containsKey && stockPointCount == 0 && !addStockMap.containsKey(key)) {
                    WarehouseVO warehouseVO = warehouseMap.get(skuStock.getStockPointId());
                    StockPointSku stockPointSku = new StockPointSku();
                    stockPointSku.setStockPointId(skuStock.getStockPointId());
                    stockPointSku.setStockPointId(skuStock.getStockPointId());
                    stockPointSku.setProdId(skuStock.getProdId());
                    if (warehouseVO.getSysType() == 3) {
                        stockPointSku.setStockPointType(StockPointType.STATION.getValue());
                        stockPointSku.setStockMode(warehouseVO.getStockMode());
                    } else {
                        stockPointSku.setStockPointType(StockPointType.WAREHOUSE.getValue());
                        stockPointSku.setType(1);
                    }
                    stockPointSku.setSkuId(skuStock.getSkuId());
                    stockPointSku.setStatus(1);
                    addStockMap.put(key, stockPointSku);
                }
            }
        }
        if (MapUtil.isNotEmpty(addStockMap)) {
            stockPointSkuService.saveBatch(addStockMap.values());
        }
        return addStockMap.values().stream().collect(Collectors.toMap(StockPointSku::getSkuId, StockPointSku::getProdId, (v1, v2) -> v1));
    }

    @Override
    public void restoreStock() {
//        stockBillLogMapper
        PageParam pageParam = new PageParam();
        pageParam.setSize(100);
        pageParam.setCurrent(1);
        while (true) {
            IPage page = skuMapper.page(pageParam);
            if (CollUtil.isEmpty(page.getRecords())) {
                break;
            }
            pageParam.setCurrent(pageParam.getCurrent() + 1);
            // 1.根据sku的数据遍历恢复库存
            List<Sku> skuList = page.getRecords();
            Set<Long> prodIdSet = skuList.stream().map(Sku::getProdId).collect(Collectors.toSet());
            List<Product> productList = productMapper.selectList(new LambdaQueryWrapper<Product>().in(Product::getProdId, prodIdSet));
            Map<Long, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getProdId, Function.identity()));
//            skuList = skuList.stream().filter(sku -> Objects.equals(sku.getSkuId(), 5983L)).collect(Collectors.toList());
//            if (CollUtil.isEmpty(skuList)) {
//                continue;
//            }
            List<Long> skuIds = skuList.stream().map(Sku::getSkuId).toList();
            List<Long> shopIds = skuList.stream().map(Sku::getShopId).toList();
            // 2.根据tz_warehouse、tz_station和tz_order_item, 确定sku的关联数据
            List<WarehouseVO> warehouseDbList = warehouseService.listWarehouseAndStationByShopIds(shopIds);
            Map<Long, List<WarehouseVO>> shopWarehouseMap = warehouseDbList.stream().collect(Collectors.groupingBy(WarehouseVO::getShopId));
            // 获取订单的sku库存数据
            List<OrderItem> orderItems = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
                    .in(OrderItem::getSkuId, skuIds)
                    .select(OrderItem::getSkuId, OrderItem::getStockPointId));
            Map<Long, List<OrderItem>> skuOrderItemMap = orderItems.stream().collect(Collectors.groupingBy(OrderItem::getSkuId));
            // sku绑定数据
            List<StockPointSku> stockPointSkus = stockPointSkuMapper.selectList(new LambdaQueryWrapper<StockPointSku>().in(StockPointSku::getSkuId, skuIds));
            Map<Long, List<StockPointSku>> skuStockPointMap = stockPointSkus.stream().collect(Collectors.groupingBy(StockPointSku::getSkuId));
            Map<String, StockPointSkuVO> stockPointSkuMap = new HashMap<>(skuList.size());
            for (Sku sku : skuList) {
                if (!shopWarehouseMap.containsKey(sku.getShopId()) || !skuStockPointMap.containsKey(sku.getSkuId())) {
                    continue;
                }
                List<WarehouseVO> warehouseList = shopWarehouseMap.get(sku.getShopId());
                List<Long> orderPointIds = Collections.emptyList();
                if (skuOrderItemMap.containsKey(sku.getSkuId())) {
                    List<OrderItem> orderItemList = skuOrderItemMap.get(sku.getSkuId());
                    orderPointIds = orderItemList.stream().map(OrderItem::getStockPointId).toList();
                }
                List<StockPointSku> stockPointSkuList = skuStockPointMap.get(sku.getSkuId());
                for (StockPointSku stockPointSku : stockPointSkuList) {
                    String key = stockPointSku.getSkuId() + Constant.UNDERLINE + stockPointSku.getStockPointId();
                    if (!stockPointSkuMap.containsKey(key) && !Objects.equals(stockPointSku.getStockMode(), StockModeEnum.ALL_STOCK.value())) {
                        stockPointSkuMap.put(key, new StockPointSkuVO(stockPointSku.getSkuId(), stockPointSku.getStockPointId()));
                    }
                }
                for (WarehouseVO warehouseVO : warehouseList) {
                    String key = sku.getSkuId() + Constant.UNDERLINE + warehouseVO.getWarehouseId();
                    boolean containPoint = orderPointIds.contains(warehouseVO.getWarehouseId()) ||
                            stockPointSkuMap.containsKey(sku.getSkuId() + Constant.UNDERLINE + warehouseVO.getWarehouseId()) ||
                            Objects.equals(warehouseVO.getStockMode(), StockModeEnum.ALL_STOCK.value());
                    if (containPoint) {
                        continue;
                    }
                    stockPointSkuMap.put(key, new StockPointSkuVO(sku.getSkuId(), warehouseVO.getWarehouseId()));
                }
            }
            Map<Long, List<StockPointSkuVO>> stockSKuMap = stockPointSkuMap.values().stream().collect(Collectors.groupingBy(StockPointSkuVO::getSkuId));
            Map<String, SkuStockVO> pointStockMap = stockManager.skuStockMap(new ArrayList<>(stockPointSkuMap.values()));
            List<StockBillLogItem> stockBillLogItems = stockBillLogItemMapper.listSkuLateStock(skuIds);
            Map<Long, List<StockBillLogItem>> billItemSkuMap = stockBillLogItems.stream().collect(Collectors.groupingBy(StockBillLogItem::getSkuId));
            Map<Long, SkuStockVO> totalStockMap = stockManager.mapSkuTotalStock(skuIds);
            List<SkuStockVO> skuStocks = orderItemMapper.countSkuSaleAndLockNum(skuIds);
            Map<Long, List<SkuStockVO>> orderItemStockMap = skuStocks.stream().collect(Collectors.groupingBy(SkuStockVO::getSkuId));
            for (Map.Entry<Long, List<StockPointSkuVO>> entry : stockSKuMap.entrySet()) {
                Long skuId = entry.getKey();
                List<StockPointSkuVO> stockPointSkuList = entry.getValue();
                SkuStockVO totalSkuStock = totalStockMap.get(skuId);
                Map<Long, Integer> billStockMap = Collections.emptyMap();
                if (billItemSkuMap.containsKey(skuId)) {
                    List<StockBillLogItem> stockBillLogItemList = billItemSkuMap.get(skuId);
                    billStockMap = stockBillLogItemList.stream().collect(Collectors.toMap(StockBillLogItem::getStockPointId, StockBillLogItem::getAfterStock));
                }
                int stock = 0;
                int sale = 0;
                int lockStock = 0;
                Map<Long, SkuStockVO> saleMap = new HashMap<>();
                if (orderItemStockMap.containsKey(skuId)) {
                    List<SkuStockVO> skuStockList = orderItemStockMap.get(skuId);
                    for (SkuStockVO skuStockVO : skuStockList) {
                        sale += skuStockVO.getSale();
                        lockStock += skuStockVO.getLockStock();
                        if (Objects.nonNull(skuStockVO.getStockPointId())) {
                            saleMap.put(skuStockVO.getStockPointId(), skuStockVO);
                        }
                    }
                }
                totalSkuStock.setSale(sale);
                totalSkuStock.setLockStock(lockStock);
                for (StockPointSkuVO stockPointSkuVO : stockPointSkuList) {
                    stockPointSkuVO.setStock(0);
                    String key = StockUtil.skuPointKey(stockPointSkuVO.getSkuId(), stockPointSkuVO.getStockPointId());
                    SkuStockVO skuStockVO = pointStockMap.get(key);
                    // sku库存区域存在，删除集合中对应的数据即可
                    if (!Objects.equals(skuStockVO.getStatus(), StatusEnum.DELETE.value())) {
                        if (Objects.equals(skuStockVO.getStatus(), StatusEnum.ENABLE.value())) {
                            stock += skuStockVO.getStock();
                        }
                        stockPointSkuMap.remove(key);
                        continue;
                    }
                    // 以下是redis中不存在的区域库存处理
                    if (saleMap.containsKey(stockPointSkuVO.getStockPointSkuId())) {
                        SkuStockVO skuSale = saleMap.get(stockPointSkuVO.getStockPointSkuId());
                        stockPointSkuVO.setSale(skuSale.getSale());
                        stockPointSkuVO.setLockStock(skuSale.getLockStock());
                    }
                    if (billStockMap.containsKey(stockPointSkuVO.getStockPointId())) {
                        // 根据库存记录中的数据恢复库存
                        stockPointSkuVO.setStock(billStockMap.get(stockPointSkuVO.getStockPointId()));
                    }
                    stock += stockPointSkuVO.getStock();
                }
                totalSkuStock.setStock(stock);
            }
            Map<String, String> dataMap = new HashMap<>(stockPointSkuMap.size());
            for (StockPointSkuVO stockPointSkuVO : stockPointSkuMap.values()) {
                String key = CacheNames.SKU_SOCK + stockPointSkuVO.getSkuId() + Constant.UNDERLINE + stockPointSkuVO.getStockPointId();
                if (Objects.isNull(stockPointSkuVO.getSale())) {
                    stockPointSkuVO.setSale(0);
                }
                if (Objects.isNull(stockPointSkuVO.getLockStock())) {
                    stockPointSkuVO.setLockStock(0);
                }
                String value = stockPointSkuVO.getStock() + Constant.UNDERLINE +
                        stockPointSkuVO.getLockStock() + Constant.UNDERLINE +
                        stockPointSkuVO.getSale() + Constant.UNDERLINE + StatusEnum.ENABLE.value();
                dataMap.put(key, value);
            }
            Map<Long, Sku> skuMap = skuList.stream().collect(Collectors.toMap(Sku::getSkuId, Function.identity()));
            for (SkuStockVO skuStockVO : totalStockMap.values()) {
                Sku sku = skuMap.get(skuStockVO.getSkuId());
                Product product = productMap.get(sku.getProdId());
                // 商品上架且sku启用，sku库存才是启用状态
                boolean skuEnable = Objects.nonNull(product) && Objects.equals(product.getStatus(), StatusEnum.ENABLE.value()) &&
                        Objects.equals(sku.getStatus(), StatusEnum.ENABLE.value());
                skuStockVO.setStatus(skuEnable ? StatusEnum.ENABLE.value() : StatusEnum.DISABLE.value());
                String key = CacheNames.SKU_SOCK + skuStockVO.getSkuId();
                String value = skuStockVO.getStock() + Constant.UNDERLINE +
                        skuStockVO.getLockStock() + Constant.UNDERLINE +
                        skuStockVO.getSale() + Constant.UNDERLINE + skuStockVO.getStatus();
                dataMap.put(key, value);
            }
            stockManager.mSet(dataMap);
        }
    }

    @Override
    public void updateSkuStockStatus(List<Long> prodIds, Integer status) {
        if (CollUtil.isEmpty(prodIds) || Objects.isNull(status)) {
            return;
        }
        // 获取商品未删除的sku列表
        List<Sku> skus = skuMapper.listSkuByProdIds(prodIds);
        List<Long> skuIds = skus.stream().map(Sku::getSkuId).toList();
        status = Objects.equals(status, StatusEnum.ENABLE.value()) ? status : StatusEnum.DISABLE.value();
        List<SkuStockVO> skuStocks = new ArrayList<>();
        for (Long skuId : skuIds) {
            SkuStockVO skuStockVO = new SkuStockVO(skuId, 0);
            skuStockVO.setStatus(status);
            skuStocks.add(skuStockVO);
        }
        stockManager.skuStock(skuStocks, LuaOperateEnum.SKU_STATUS);
    }

    private void loadSeckillStockBillLogItems(List<StockPointSkuDTO> filtePointList, Long shopId) {
        List<Long> skuIds = filtePointList.stream().map(StockPointSkuDTO::getSkuId).collect(Collectors.toList());
        List<Sku> skuList = skuMapper.selectList(new LambdaQueryWrapper<>(Sku.class).in(Sku::getSkuId, skuIds));
        Map<Long, List<StockPointSkuDTO>> skuStockMap = filtePointList.stream().collect(Collectors.groupingBy(StockPointSkuDTO::getSkuId));
        skuList.forEach(s -> s.setStockPointList(skuStockMap.get(s.getSkuId())));
        List<StockBillLogItemDTO> stockBillLogItems = new ArrayList<>(filtePointList.size());
        Map<Long, Sku> skuMap = skuList.stream().collect(Collectors.toMap(Sku::getSkuId, s -> s));
        filtePointList.forEach(s -> {
            Sku sku = skuMap.get(s.getSkuId());
            StockBillLogItemDTO stockBillLogItem = new StockBillLogItemDTO(
                    s.getProdId(),
                    s.getSkuId(),
                    shopId,
                    null,
                    sku.getProdName(),
                    sku.getSkuName(),
                    s.getStock(),
                    sku.getPrice(),
                    sku.getPartyCode(),
                    sku.getPic()
            );
            stockBillLogItem.setAfterStock(0);
            stockBillLogItem.setStockBillType(StockBillType.SECKILL_STOCK.value());
            stockBillLogItem.setStocktype(StockType.WAREHOUSING.value());
            stockBillLogItem.setStockPointId(s.getStockPointId());
            stockBillLogItem.setStockPointType(s.getStockPointType());
            stockBillLogItem.setMakerMobile(null);
            stockBillLogItems.add(stockBillLogItem);
        });
        // 发送事件进行添加商品入库记录
        applicationContext.publishEvent(new StockBillChangeEvent(stockBillLogItems, StockBillType.SECKILL_STOCK.value(), StockType.WAREHOUSING.value()));
    }
}
